﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
using System.Xml.XPath;
using System.Windows.Forms;

namespace EVEApi
{
    public class API
    {

        public event EventHandler APIBereit;

        string Para = "?KeyID=110269&vCode=mvvRMsNNp1kb6mYHxP5jKdZU5CfU8P4dq8epvcMziGPLPOQ6ymtCE9GOSK6QrXZd";
        System.Data.DataSet DS = new System.Data.DataSet();



        public void DownloadXML(string URL, String Parameter, String XMLName)
        {

            try {
System.Net.WebClient WC = new System.Net.WebClient();
            if (!WC.IsBusy) {
                WC.DownloadFile(URL + Parameter, XMLName);
             
            }
            
             if (APIBereit != null) {
                            APIBereit(this, EventArgs.Empty);

            }
            } catch (System.Net.WebException ex) {
                
             MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);   
            }
            

        }

      
        public DataTable CharacterLoad()
        {

            string URL = "https://api.eveonline.com/account/Characters.xml.aspx";
            DownloadXML(URL, Para, "Chara.XML");
            FileStream FS = new FileStream("Chara.xml", FileMode.Open);
            XmlTextReader XTR = new XmlTextReader(FS);
 
            System.Data.DataTable DT = new System.Data.DataTable();
            DT = DataTableErstellen(XTR, "Character");

           


            FS.Close();
            return DT;
        }

        public DataTable TransactionLoad(string CharaID)
        {
            String URL = "https://api.eveonline.com/char/WalletTransactions.xml.aspx";
            DownloadXML(URL, Para + "&characterID=" + CharaID, "Wallet.XML");
            FileStream FS = new FileStream("Wallet.xml", FileMode.Open);
            XmlTextReader XTR = new XmlTextReader(FS);
            DataTable DT = new DataTable();
            DT = DataTableErstellen(XTR, "Transaction");

            return DT;

        }
        private DataTable DataTableErstellen(XmlTextReader XTR, string DataTableName)
        {

            DataTable DT = new DataTable();
            DT.TableName = DataTableName;
            List<string> Columns = new List<string>();
            XPathDocument XPD = new XPathDocument(XTR);
            XPathNavigator XPN = XPD.CreateNavigator();
            while (XPN.MoveToFollowing("rowset", "")) {

                Columns.AddRange(XPN.GetAttribute("columns", "").Split(','));

            }
            foreach (String item in Columns) {
                DT.Columns.Add(item);
                if (DT.TableName == "Transaction")
                {
                    if (item == "price")
                    {
                                          DT.Columns["price"].DataType = Type.GetType("System.Decimal");
  
                    }

                }

            }
            while (XPN.MoveToFollowing("row", "")) {
                DataRow DR;
                DR = DT.NewRow();
                foreach (String item in Columns) {
                    if (item == "price")
                    {
                        DR[item] = Convert.ToDouble(XPN.GetAttribute(item, ""), System.Globalization.CultureInfo.CreateSpecificCulture("en-us"));

                    }
                    else
                    {
                        DR[item] = XPN.GetAttribute(item, "");

                    }
                }

                DT.Rows.Add(DR);


            }
            if (DT.TableName == "Transaction")
            {
                System.Diagnostics.Debug.WriteLine(DT.Columns["price"].DataType);
   
            }
           return DT;
        }
    }
}
